//
//  BIDEditGenderViewController.m
//  HJGJCommunity
//
//  Created by mal on 2017/6/17.
//  Copyright © 2017年 mal. All rights reserved.
//

#import "BIDEditGenderViewController.h"

static NSString *editGenderURL = @"memberAndroid/modify.action";

@interface BIDEditGenderViewController ()
{
    NSArray *_dataSourceArr;
    NSInteger _selectIndex;
}
@end

@implementation BIDEditGenderViewController
@synthesize gender;
@synthesize selectIndexPath;
@synthesize delegate;

- (void)viewDidLoad {
    [super viewDidLoad];
    _selectIndex = -1;
    self.title = @"修改性别";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(saveHandler)];
    _dataSourceArr = @[@{@"gender":@"男", @"code":@"1"},
                       @{@"gender":@"女", @"code":@"2"},
                       @{@"gender":@"保密", @"code":@"3"}];
    _tableView.tableFooterView = [[UIView alloc] init];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)saveHandler
{
    if(_selectIndex < 0)
    {
        [self showAlertView:@"请先选择性别" buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
        return;
    }
    
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], editGenderURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"memberId=%@&memberDetail.gender=%@", [AppDelegate getUserId], _dataSourceArr[_selectIndex][@"code"]];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary *responseDic, NSString *errMsg){
        if(errMsg)
        {
            [self showAlertView:errMsg buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
        }
        else
        {
            [self showAlertView:@"保存成功" buttonTitle:ALERT_BTN_TITLE tag:@100 functionName:@"alertViewHandler"];
        }
    }];
}

- (void)alertViewHandler
{
    [delegate editGender:_dataSourceArr[_selectIndex][@"code"] atIndexPath:selectIndexPath];
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    static NSString *identifier = @"identifier";
    NSUInteger row = indexPath.row;
    cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if(!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    NSDictionary *dic = _dataSourceArr[row];
    cell.textLabel.text = dic[@"gender"];
    if(gender)
    {
        if([gender isEqualToString:dic[@"gender"]])
        {
            _selectIndex = row;
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
        else
        {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    _selectIndex = indexPath.row;
    gender = _dataSourceArr[indexPath.row][@"gender"];
    [_tableView reloadData];
}

@end
